const Base = require('./base.js');

module.exports = class extends Base {
    /**
     *  购物车数量
     * @returns {Promise<void>}
     */
    async countAction() {
        const count = await this.model('cart').where({
            user_id: think.user.user_id
        }).count();
        return this.success(count);
    }

    /**
     *  购物车列表
     * @returns {Promise<void>}
     */
    async listAction() {
        const list = await this.model('cart').alias('a').field('a.*,b.id as goods_id,b.custom_id,b.goods_name,b.goods_no,b.sale_price,b.primary_pic_url').join({
            table: 'goods', join: 'left', as: 'b', on: ['goods_id', 'id']
        }).where({user_id: think.user.user_id}).select();
        return this.success(list);
    }

    /**
     *  添加到购物车
     * @returns {Promise<void>}
     */
    async addAction() {
        const d = this.post();
        // console.log(d);
        // if (Number(d.category_id) === 1036008) {
        //     d.style = 'A';
        // }
        // if (Number(d.category_id) === 1036009) {
        //     d.style = 'B';
        // }
        // if (Number(d.category_id) === 1036010) {
        //     d.style = 'C';
        // }
        await this.model('cart').add({
            user_id: think.user.user_id,
            user_id: think.user.user_id,
            goods_id: d.goods_id,
            number: d.number,
            custom_style: d.custom_style,
            check_style: d.check_style,
            check_content: d.check_content,
            size_id: d.size_id,
            size_username: d.size_username,
            category_id: d.category_id,
            checked: 1
        });
        return this.success('添加成功');
    }

    /**
     *  修改
     * @returns {Promise<void>}
     */
    async updateAction() {
        const d = this.post();
        console.log(d)
        const user_id = think.user.user_id;
        const list = await this.model('cart').where({user_id: user_id, id: d.cart_id}).find();
        if (think.isEmpty(list)) {
            return this.fail('此商品不存在');
        }
        /**
         * type=1 是否选择修改
         * type=2 数量修改
         * type=3 尺寸修改
         * type=4 定制参数修改
         */
        if (d.type === 1) {
            await this.model('cart').where({
                user_id: user_id, id: d.cart_id
            }).update({checked: d.checked});
        } else if (d.type === 2) {
            if (d.number <= 0) {
                await this.model('cart').where({
                    user_id: user_id, id: d.cart_id
                }).delete();
            } else {
                await this.model('cart').where({
                    user_id: user_id, id: d.cart_id
                }).update({number: d.number});
            }
        } else if (d.type === 3) {
            await this.model('cart').where({
                user_id: user_id, id: d.cart_id
            }).update({size_id: d.size_id, size_username: d.size_username});
        } else if (d.type === 4) {
            await this.model('cart').where({
                user_id: user_id, id: d.cart_id
            }).update({custom_style: d.custom_style, check_style: d.check_style, check_content: d.check_content});
        }

        // const result = await this.model('cart').alias('a').field('a.*,b.id as goods_id,b.custom_id,b.goods_name,b.goods_no,b.sale_price,b.primary_pic_url').join({
        //     table: 'goods', join: 'left', as: 'b', on: ['goods_id', 'id']
        // }).where({user_id: think.user.user_id}).select();
        return this.success(true);
    }

    /**
     * 清空当前购物车
     * @returns {Promise<void>}
     */
    async clearAction() {
        await this.model('cart').where({
            user_id: think.user.user_id
        }).delete();
        return this.success(true);
    }
};
